var auths = [],
	ws;
document.addEventListener('plusready', function() {
	ws = plus.webview.currentWebview();
	var spage = ws.spage;
	var ads = ws.ads;
	var store_id = plus.storage.getItem('storeid');
	var key = plus.storage.getItem('key');
	if(key) {
		relogin(ws.id);
		return;
	}
	
	$('.pass').on('click',function(){
		var user = $('.phone').val();
		console.log(user);
		if(!user){
			return;
		} 
		console.log(1);
		$.ajax({
			type:"post",
			url:apiRoot + "?act=seller_login&op=avatar",
			data:{
				seller_name:user
			}, 
			dataType:'json',
			success:function(data){
				console.log(JSON.stringify(data));
				if(data.code == 200){
					$(".member_avatar").attr('src',user_pic + data.datas.member_avatar);
				}else{
					toast(data.datas.error);
				}
			}
		});
	})
	

	$('.res_pasw').on('click', function() {
		var user = $('.phone').val();
		var pass = $('.pass').val();
		if(!user || !pass) {
			plus.nativeUI.toast('账号或密码不能为空');
			return;
		}
		console.log(user+'--'+pass);
		plus.nativeUI.showWaiting('登录中...');
		$.ajax({
			type: "post",
			url: apiRoot + "?act=seller_login&op=index",
			data: {
				seller_name: user,
				password: pass,
				client:'wap'
			},
			dataType: 'json',
			success: function(data) {
				plus.nativeUI.closeWaiting('');
				console.log(JSON.stringify(data));
				if(data.code == 200) {
					toast('登陆成功');
					plus.storage.setItem('store_name', data.datas.store_name + '');
					plus.storage.setItem('storeid', data.datas.seller_id + '');
					plus.storage.setItem('avatar', data.datas.member_avatar + '');
					plus.storage.setItem('member_id', data.datas.member_id + '');
					plus.storage.setItem('key', data.datas.key + '');
					relogin(ws.id);
				} else {
					toast(data.datas.error);
				}
			},
			error: function(e) {
				plus.nativeUI.closeWaiting();
				plus.nativeUI.toast("连接失败，请重试。");
				//			 	plus.nativeUI.toast('失去连接1');return;
			}
		});
	})
	//退出登录的判断
	if(spage) {
		var first = null;
		mui.back = function() {
			//首次按键，提示‘再按一次退出应用’
			if(!first) {
				first = new Date().getTime();
				mui.toast('再按一次退出应用');
				setTimeout(function() {
					first = null;
				}, 2000);
			} else {
				if(new Date().getTime() - first < 2000) {
					plus.runtime.quit();
				}
			}
		};
	}
	//退出登录的判断
	if(ads) {
		var first = null;
		mui.back = function() {
			//首次按键，提示‘再按一次退出应用’
			if(!first) {
				first = new Date().getTime();
				mui.toast('再按一次退出应用');
				setTimeout(function() {
					first = null;
				}, 2000);
			} else {
				if(new Date().getTime() - first < 2000) {
					plus.runtime.quit();
				}
			}
		};
	}

}, false);